package test;

/**
 * @author : RobertWei
 * time: 2021/9/27    14:19
 * description:
 */

public class Demo {

    public static void main(String[] args) {

        int[] heights = {1,2,5,4,5,2,5};

        int largestRectangleArea = largestRectangleArea(heights);

        System.out.println("largestRectangleArea = " + largestRectangleArea);

    }

    public static int largestRectangleArea(int[] heights) {


        //为空直接返回 0
        if (heights.length == 0){
            return 0;
        }

        //实时存储最大值
        int largest = 0;

        //从第一个高度开始向后计算最大面积
        for (int i = 0; i < heights.length; i++) {

            //实时存储前方可用最高高度
            int height = heights[i];

            //自身高度与当前最大值做对比
            largest = Math.max(height , largest);

            //从第 i + 1 个向后遍历高度 计算最大面积
            for (int j = i + 1; j < heights.length; j++) {

                //高度更改为当前遍历之后的最小值
                height = Math.min(height, heights[j]);

                //存储的最大值与 此时面积进行对比
                largest = Math.max( largest , height * (j - i + 1) );

            }

        }
        return largest;
    }

}
